<?php

namespace addons\qingdongams\controller;

use addons\qingdongams\model\Product as ProductModel;
use addons\qingdongams\model\ProductType;
use think\Db;
use think\Exception;
/**
 * @desc 操作文档：https://doc.fastadmin.net/qingdongams
 * @desc 软件介绍：https://www.fastadmin.net/store/qingdongams.html
 * @desc 售后微信：qingdong_crm
 */

/**
 * 产品
 */
class Product extends StaffApi {
	protected $noNeedLogin = [];
	protected $noNeedRight = [];


	//获取产品记录
	public function getList() {
		$limit  = input("limit/d", 10);
		$params = $this->request->post();
		$where  = [];
		$order  = 'id desc';
		if (isset($params['name']) && $params['name']) {//产品名称
			$where['name|num'] = ['like', "%{$params['name']}%"];
		}

		if (isset($params['type_id']) && $params['type_id']) {//产品名称
            $typeids=ProductType::where(['pid'=>$params['type_id']])->whereOr(['id'=>$params['type_id']])->column('id');
			$where['type_id'] = ['in',$typeids];
		}

		$list              = ProductModel::where($where)->field('description',true)->order($order)->paginate($limit);
		$this->success('请求成功', $list);
	}


	//获取select产品列表
	public function getSelectList() {
		$name  = input('name');
		$where = [];
		if ($name) {
			$where['name|num'] = ['like', "%{$name}%"];
		}
        if (isset($params['type_id']) && $params['type_id']) {//产品名称
            $typeids=ProductType::where(['pid'=>$params['type_id']])->column('id');
            $where['type_id'] = ['in',$typeids];
        }

		$list              = ProductModel::where($where)->select();
		$this->success('请求成功', $list);
	}


	//添加产品
	public function addProduct() {
		$params = $this->request->post();
		// 表单验证
		if (($result = $this->qingdongamsValidate($params,get_class(), 'create')) !== true) {
			$this->error($result);
		}
		if (ProductModel::where(['name' => $params['name']])->find()) {
			$this->error('产品名称已存在');
		}

		try {
			$product            = ProductModel::createProduct($params);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			$this->error($e->getMessage());
		}
		if ($result) {
			$this->success('新增产品成功');
		}
	}


	//修改产品
	public function editProduct() {
		$id     = input('id');
		$params = $this->request->post();
		$row    = ProductModel::where(['id' => $id])->find();
		if (empty($row)) {
			$this->error('修改产品信息不存在');
		}
		// 表单验证
		if (($result = $this->qingdongamsValidate($params,get_class(), 'edit')) !== true) {
			$this->error($result);
		}
		Db::startTrans();
		try {
			$result = ProductModel::updateProduct($params);
			Db::commit();
		} catch (Exception $e) {
			Db::rollback();
			$this->error($e->getMessage());
		}
		$this->success('修改产品信息成功');
	}


	//获取产品详情
	public function getProductDetail(){
	    $id=input('id');
	    $product=ProductModel::where(['id'=>$id])->find();
	    if(empty($product)){
	        $this->error('产品信息不存在');
        }
	    $this->success('请求成功',$product);
    }


    //获取产品分类
    public function getProductType(){
       $types= ProductType::where(['pid'=>0])->field('id,name')->select();
        $this->success('请求成功',$types);
    }


}
